package com.example.back.controller.otd;

import com.example.back.entity.Browse;
import com.example.back.entity.otd.PostAndUser;
import com.example.back.entity.otd.Posts;
import com.example.back.mapper.BrowseMapper;
import com.example.back.mapper.PostMapper;
import com.example.back.mapper.UserMapper;
import com.example.back.mapper.otd.PostsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
public class PostAndUserController {
    @Autowired
    private PostMapper PostMapper;
    @Autowired
    private UserMapper UserMapper;
    @Autowired
    private PostsMapper PostsMapper;
    @Autowired
    private BrowseMapper BrowseMapper;
    @GetMapping("/postAndUser/{id}")
    public PostAndUser queryPostAndUser(@PathVariable("id") Integer userId){

        PostAndUser postAndUser = new PostAndUser();
        postAndUser.setPosts(PostMapper.queryPostByUserId(userId));
        postAndUser.setUser(UserMapper.queryUserById(userId));
        return postAndUser;
    }
    @GetMapping("/postAndUser")
    public ArrayList<Posts> queryPostAndUser(){
        return PostsMapper.getPosts();
    }
    @GetMapping("/postAndUser/fanId/{fanId}")
    public ArrayList<Posts> queryPostAndUserByfanId(@PathVariable("fanId") Integer fanId){
        return PostsMapper.getPostsByfanId(fanId);
    }
    @GetMapping("/postAndUser/new")
    public ArrayList<Posts> queryNewPostAndUser(){
        return PostsMapper.getNewPosts();
    }
    @GetMapping("/postAndUser/id/{id}/{userId}")
    public Posts queryPostAndUserById(@PathVariable("id") Integer id ,@PathVariable("userId") Integer userId){
       Browse browse = BrowseMapper.queryBrowse(id,userId);
       if(browse == null){
           BrowseMapper.addBrowse(id,userId);
           PostMapper.addBrowse(id);
           PostMapper.addDayBrowse(id);
       }
        return PostsMapper.queryPostById(id);
    }

    //====================
    @GetMapping("/postAndUser/allCount")
    public ArrayList<Posts> queryPostAndUserWithAllCounts(){
        return PostsMapper.getPostsWithAllCounts();
    }

    @GetMapping("/postAndUser/allCount/time")
    public ArrayList<Posts> queryPostAndUserWithAllCountsOrderedByTime(){
        return PostsMapper.getPostsWithAllCountsOrderedByTime();
    }
}
